#include "KeyGetWindow.h"
#include "NotifWindow.h"
#include "WindowManager.h"
#include "Satory.h"
#include "ui_KeyGetWindow.h"
KeyGetWindow::KeyGetWindow(QWidget *parent) :
    BaseWindow(parent),
    ui(new Ui::KeyGetWindow)
{
    ui->setupUi(this);
    QObject::connect(ui->UpdateSearchButton,SIGNAL(clicked()),this,SLOT(OnUpdateSearchClick()));
    QObject::connect(ui->DownloadButton,SIGNAL(clicked()),this,SLOT(OnDownloadClick()));
    QObject::connect(ui->CreateButton,SIGNAL(clicked()),this,SLOT(OnCreateClick()));
    QObject::connect(ui->ReadyButton,SIGNAL(clicked()),this,SLOT(OnReadyClick()));
    SetMode(UPDATE);

}

void KeyGetWindow::SetIncomingData(QString login,QString pass)
{
    userName=login;
    userPass=pass;
}

void KeyGetWindow::SetMode(int val)
{

    mode=val;
    if(val==RESET || val==DOWNLOAD)
        this->setFixedHeight(270);
    else
        this->setFixedHeight(100);
    if(val==RESET || val==DOWNLOAD)
    {
        ui->ReadyButton->setVisible(true);
        ui->CodeLabel->setVisible(true);
        ui->codeLine->setVisible(true);
        if(val==RESET)
        {
            ui->PassLabel->setVisible(true);
            ui->passLine->setVisible(true);
            ui->storeCheck->setVisible(true);
        }
        else
        {
            ui->PassLabel->setVisible(false);
            ui->passLine->setVisible(false);
            ui->storeCheck->setVisible(false);
        }
     }
    else
    {
        ui->ReadyButton->setVisible(false);
        ui->CodeLabel->setVisible(false);
        ui->codeLine->setVisible(false);
        ui->PassLabel->setVisible(false);
        ui->passLine->setVisible(false);
    }


}

void KeyGetWindow::OnUpdateSearchClick()
{

    SetMode(UPDATE);

   if(Satory::GetInstance()->Login(userName,userPass))
    WindowManager::GetInstance()->CloseWindow(WindowManager::KEYGETWINDOW);
}
void KeyGetWindow::OnDownloadClick()
{

    SetMode(DOWNLOAD);

    Satory::GetInstance()->Verify(userName);
}
void KeyGetWindow::OnCreateClick()
{

    SetMode(RESET);


   Satory::GetInstance()->Verify(userName);


}
void KeyGetWindow::OnReadyClick()
{

    if(mode==RESET)
    {
        if(!Satory::GetInstance()->ResetKey(ui->codeLine->text(),userName,ui->passLine->text(),ui->storeCheck->isChecked()))
        {
            NotifWindow* win=( NotifWindow*)WindowManager::GetInstance()->ShowWindow(WindowManager::NOTIFWINDOW);
            win->SetText("Wait for server response");
        }
     }
    if(mode==DOWNLOAD)
    {
        if(!Satory::GetInstance()->PrivateKeyGet(ui->codeLine->text(),userName))
        {
            NotifWindow* win=(NotifWindow*)WindowManager::GetInstance()->ShowWindow(WindowManager::NOTIFWINDOW);
            win->SetText("Wait for server response");
        }
    }

}
KeyGetWindow::~KeyGetWindow()
{
    delete ui;

 }

